import { CARTLIST, SELECTIDS, SELECTEDPROLIST, SESSIONSELECT, SHOPCARTNUM } from './mutations-types'
import {setSession, getSession} from '@/config/mUntils'
export default{
  [CARTLIST] (state, list) {
    if (list.resultCode === 2000) {
      state.cartlist = list.data
      let cartLists = list.data
      let limitArr = []
      let arr = []
      if (cartLists) {
        cartLists.map((item, index) => {
          if (index === 0) {
            state.shopCartId = item.id
            state.cartlength = item.productList.length
          }
          let productList = item.productList
          if (productList && productList.length > 0) {
            productList.map((proItem, proIdx) => {
              let obj = {}
              // 所有ids
              arr.push(proItem.id)
              // 限购ids
              if (proItem.personalLimited) {
                obj['pid'] = proItem.productId
                obj['num'] = proItem.number
                limitArr.push(obj)
              }
            })
          }
        })
        state.limitArr = limitArr
        state.shopcartIds = arr
      }
    }
  },
  [SELECTIDS] (state, str) {
    setSession('selecteds', str)
    state.selectedIds = str
  },
  [SELECTEDPROLIST] (state, arr) {
    setSession('selectedProList', arr)
    state.selectedProList = arr
  },
  [SESSIONSELECT] (state) {
    let _arr = getSession('selectedProList')
    state.selectedProList = JSON.parse(_arr)  // session中对content 进行JSON.stringify
  },
  [SHOPCARTNUM] (state, value) {
    state.shopcartnum = value - 0
  }
}
